﻿#include "asycipc/telemetry.hpp"

namespace asycipc {

void TelemetryCenter::IncrementCounter(const std::string& name,
                                        uint64_t delta) {
  std::lock_guard<std::mutex> lock(mutex_);
  counters_[name] += delta;
}

uint64_t TelemetryCenter::GetCounter(const std::string& name) const {
  std::lock_guard<std::mutex> lock(mutex_);
  auto it = counters_.find(name);
  return it == counters_.end() ? 0 : it->second;
}

void TelemetryCenter::SetGauge(const std::string& name, double value) {
  std::lock_guard<std::mutex> lock(mutex_);
  gauges_[name] = value;
}

double TelemetryCenter::GetGauge(const std::string& name) const {
  std::lock_guard<std::mutex> lock(mutex_);
  auto it = gauges_.find(name);
  return it == gauges_.end() ? 0.0 : it->second;
}

void TelemetryCenter::ObserveHistogram(const std::string& name, double value) {
  std::lock_guard<std::mutex> lock(mutex_);
  histograms_[name].push_back(value);
}

std::vector<double> TelemetryCenter::GetHistogram(
    const std::string& name) const {
  std::lock_guard<std::mutex> lock(mutex_);
  auto it = histograms_.find(name);
  if (it == histograms_.end()) {
    return {};
  }
  return it->second;
}

}  // namespace asycipc
